//
//  ReChargeViewController.m
//  YYPHSpeedJump
//
//  Created by sujiang on 16/1/7.
//  Copyright © 2016年 sujiang. All rights reserved.
//

#import "ReChargeViewController.h"

@interface ReChargeViewController ()<UIAlertViewDelegate>

@property (nonatomic, strong) NSString *payWay;

@end

@implementation ReChargeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AliPayResultNotify:) name:@"Alipay" object:nil];
    self.payWay = @"zfb";
    self.title = @"充值";
    // Do any additional setup after loading the view from its nib.
    self.moneyTF.keyboardType = UIKeyboardTypeNumberPad;
}

- (NSString *)creatOrderNO
{
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYYMMDDhhmmss"];
    NSString * string = [formatter stringFromDate:date];
    NSString *tradeNO = [NSString stringWithFormat:@"%@%@", string, [[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]];
    NSLog(@"%@", tradeNO);
    return tradeNO;
    
}



#pragma mark -- 按钮点击事件
// 支付宝
- (IBAction)zfbSelectBtn:(id)sender {
    self.zfbBtn = sender;
    [self.zfbBtn setBackgroundImage:[UIImage imageNamed:@"xz"] forState:(UIControlStateNormal)];
    //[self.wxBtn setBackgroundImage:[UIImage imageNamed:@"wxdx"] forState:(UIControlStateNormal)];
    self.payWay = @"zfb";
}
// 微信
//- (IBAction)wxSelectBtn:(id)sender {
//    [self.zfbBtn setBackgroundImage:[UIImage imageNamed:@"wxdx"] forState:(UIControlStateNormal)];
//    //[self.wxBtn setBackgroundImage:[UIImage imageNamed:@"xz"] forState:(UIControlStateNormal)];
//    self.payWay = @"wx";
//}
// 确认
- (IBAction)sureBtn:(id)sender {
    // 支付宝充值接口trade_no = YYYYMMddHHmmss+UID, notify_url=http://121.42.156.174:8080/speedJump/alipayNotify.do
    if ([self.payWay isEqualToString:@"zfb"]) {
        
        [[ZFBPayManager shareManager] alipayWithOrderProductName:@"速降充值" notifyUrl:kPayNotifyUrl productDescription:@"速降商品充值" money:self.moneyTF.text paymentType:@"1" tradeNO:[self creatOrderNO]];
        
        
    } else if ([self.payWay isEqualToString:@"wx"]) {
        
    }
    
    
}

-(void)AliPayResultNotify:(NSNotification *)notify
{
    NSDictionary *dict = [notify userInfo];
    [self AliPayResult:dict];
}

#pragma mark -
#pragma mark    ----支付宝回调结果结果判断-----
-(void)AliPayResult:(NSDictionary *)resultDic
{
    /*
     9000 订单支付成功
     8000 正在处理中
     4000 订单支付失败
     6001 用户中途取消
     6002 网络连接出错
     */
    NSLog(@"result = %@", resultDic[@"resultStatus"]);
    NSDictionary *stateDict = @{@"9000":@"订单支付成功",@"8000":@"正在处理中",@"4000":@"订单支付失败",@"6001":@"用户中途取消",@"6002":@"网络连接出错"};
    
    if([resultDic[@"resultStatus"] isEqualToString:@"9000"]){//支付成功
        //支付成功返回上一页
        __weak typeof(ReChargeViewController *)myself = self;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            
            //            [bself.navigationController popViewControllerAnimated:YES];
            [myself.navigationController popViewControllerAnimated:YES];
        });
        
    }else {
        NSString *msg = [stateDict valueForKey:resultDic[@"resultStatus"]];
        [self alert:@"提示" msg:msg];
    }
    
}

//客户端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alter show];
}


- (void)alertView:(nonnull UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([alertView.message isEqualToString:@"充值成功"] ||[alertView.message isEqualToString:@"支付结果：成功！"]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)dealloc
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
